﻿using System;
using System.IO;
using System.Reflection;
using PluginInterface;
using System.Collections.Generic;

namespace CalculadoraSimple
{
class Host{
    protected List<IPlugin> Lista;

    public Host(){
            Lista = new List<IPlugin>();
            this.Actualizar();
    }


    public void Actualizar(){
            Lista.Clear();
            String aux = AppDomain.CurrentDomain.BaseDirectory;

            foreach (string arch in Directory.GetFiles(aux)){
                FileInfo file= new FileInfo(arch);
                if (file.Extension.Equals(".dll")){//si la extension del archivo es dll:
                    Assembly pluginAssembly = Assembly.LoadFrom(arch);
                    foreach (Type pluginType in pluginAssembly.GetTypes()){
                        if (pluginType.IsPublic && !pluginType.IsAbstract){
                            Type typeInterface = pluginType.GetInterface("PluginInterface.IPlugin", true);
                            if (typeInterface != null){
                                IPlugin MiPlugin = (IPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                                Lista.Add((IPlugin)MiPlugin);
                            }
                        }
                    }
                }
            }
        }


        public List<IPlugin> getPlugins()
        {
            return Lista;
        }
    }
}
